package com.ruoyi.zsyh.controller;

import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.zsyh.domain.ZsyhAgentOrder;
import com.ruoyi.zsyh.domain.ZsyhAgentOrderRel;
import com.ruoyi.zsyh.service.IZsyhAgentOrderRelService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zsyh.domain.ZsyhPlatformPartsattribute;
import com.ruoyi.zsyh.service.IZsyhPlatformPartsattributeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 产品规格管理Controller
 * 
 * @author yuexl
 * @date 2021-12-10
 */
@Controller
@RequestMapping("/zsyh/partsattribute")
public class ZsyhPlatformPartsattributeController extends BaseController
{
    private String prefix = "zsyh/partsattribute";

    @Autowired
    private IZsyhPlatformPartsattributeService zsyhPlatformPartsattributeService;
    @Autowired
    private IZsyhAgentOrderRelService zsyhAgentOrderRelService;

    @RequiresPermissions("zsyh:partsattribute:view")
    @GetMapping()
    public String partsattribute()
    {
        return prefix + "/partsattribute";
    }

    /**
     * 查询产品规格管理列表
     */
    @RequiresPermissions("zsyh:partsattribute:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZsyhPlatformPartsattribute zsyhPlatformPartsattribute)
    {
        startPage();
        zsyhPlatformPartsattribute.setOperateType("search");
        List<ZsyhPlatformPartsattribute> list = zsyhPlatformPartsattributeService.selectZsyhPlatformPartsattributeList(zsyhPlatformPartsattribute);
        return getDataTable(list);
    }

    /**
     * 导出产品规格管理列表
     */
    @RequiresPermissions("zsyh:partsattribute:export")
    @Log(title = "产品规格管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZsyhPlatformPartsattribute zsyhPlatformPartsattribute)
    {
        List<ZsyhPlatformPartsattribute> list = zsyhPlatformPartsattributeService.selectZsyhPlatformPartsattributeList(zsyhPlatformPartsattribute);
        ExcelUtil<ZsyhPlatformPartsattribute> util = new ExcelUtil<ZsyhPlatformPartsattribute>(ZsyhPlatformPartsattribute.class);
        return util.exportExcel(list, "产品规格管理数据");
    }

    /**
     * 新增产品规格
     */
    @GetMapping("/add/{partsId}")
    public String add(@PathVariable("partsId") String partsId, ModelMap mmap)
    {
        mmap.put("partsId",partsId);
        return prefix + "/add";
    }

    /**
     * 新增保存产品规格管理
     */
    @RequiresPermissions("zsyh:partsattribute:add")
    @Log(title = "产品规格管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZsyhPlatformPartsattribute zsyhPlatformPartsattribute)
    {
        //判断名字是否重复
        ZsyhPlatformPartsattribute zsyhPlatformPartsattribute1 = new ZsyhPlatformPartsattribute();
        zsyhPlatformPartsattribute1.setAttributeName(zsyhPlatformPartsattribute.getAttributeName());
        zsyhPlatformPartsattribute1.setPartsId(zsyhPlatformPartsattribute.getPartsId());
        zsyhPlatformPartsattribute1.setOperateType("edit");
        if(zsyhPlatformPartsattributeService.selectZsyhPlatformPartsattributeList(zsyhPlatformPartsattribute1).size()>0){
            return  error("该分类下面已经存在同样的产品名称！");
        }
        //添加排序，默认在当前最大的排序字段下一个数字
        ZsyhPlatformPartsattribute zsyhPlatformPartsattribute2 = new ZsyhPlatformPartsattribute();
        zsyhPlatformPartsattribute2.setPartsId(zsyhPlatformPartsattribute.getPartsId());
        List<ZsyhPlatformPartsattribute> zsyhPlatformPartsattributeList = zsyhPlatformPartsattributeService.selectZsyhPlatformPartsattributeList(zsyhPlatformPartsattribute2);
        if(zsyhPlatformPartsattributeList.size()>0){
            zsyhPlatformPartsattribute.setAttributeSort(zsyhPlatformPartsattributeList.get(0).getAttributeSort()+1);
        }else{
            zsyhPlatformPartsattribute.setAttributeSort(1l);
        }
        return toAjax(zsyhPlatformPartsattributeService.insertZsyhPlatformPartsattribute(zsyhPlatformPartsattribute));
    }

    /**
     * 修改产品规格管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhPlatformPartsattribute zsyhPlatformPartsattribute = zsyhPlatformPartsattributeService.selectZsyhPlatformPartsattributeById(id);
        mmap.put("zsyhPlatformPartsattribute", zsyhPlatformPartsattribute);
        return prefix + "/edit";
    }

    /**
     * 修改保存产品规格管理
     */
    @RequiresPermissions("zsyh:partsattribute:edit")
    @Log(title = "产品规格管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZsyhPlatformPartsattribute zsyhPlatformPartsattribute)
    {
        //判断名字是否重复
        ZsyhPlatformPartsattribute zsyhPlatformPartsattribute1 = new ZsyhPlatformPartsattribute();
        zsyhPlatformPartsattribute1.setAttributeName(zsyhPlatformPartsattribute.getAttributeName());
        zsyhPlatformPartsattribute1.setPartsId(zsyhPlatformPartsattribute.getPartsId());
        zsyhPlatformPartsattribute1.setOperateType("edit");
        List<ZsyhPlatformPartsattribute> zsyhPlatformPartsattributeList = zsyhPlatformPartsattributeService.selectZsyhPlatformPartsattributeList(zsyhPlatformPartsattribute1);
        if(zsyhPlatformPartsattributeList.size()>1){
            if(!zsyhPlatformPartsattribute.getId().equals(zsyhPlatformPartsattributeList.get(0).getId())){
                return  error("该产品已经存在同样的规格名称！");
            }
        }
        return toAjax(zsyhPlatformPartsattributeService.updateZsyhPlatformPartsattribute(zsyhPlatformPartsattribute));
    }

    /**
     * 删除产品规格管理
     */
    @RequiresPermissions("zsyh:partsattribute:remove")
    @Log(title = "产品规格管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        //判断是否有引用
        ZsyhAgentOrderRel zsyhAgentOrderRel = new ZsyhAgentOrderRel();
        zsyhAgentOrderRel.setAttributeId(ids);
        if(zsyhAgentOrderRelService.selectZsyhAgentOrderRelList(zsyhAgentOrderRel).size()>0){
            return error("产品已经再使用，不能删除");
        }
        return toAjax(zsyhPlatformPartsattributeService.deleteZsyhPlatformPartsattributeByIds(ids));
    }

    /**
     * 查看产品规格
     */
    @GetMapping("/view/{id}")
    public String view(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("mid", id);
        return prefix + "/view";
    }

    /**
     * 指定产品规格
     */
    @GetMapping("/moveChange/{partsId}/{oldId}")
    public String moveChange(@PathVariable("partsId") String partsId,@PathVariable("oldId") String oldId, ModelMap mmap)
    {
        mmap.put("partsId", partsId);
        mmap.put("oldId", oldId);
        return prefix + "/moveChange";
    }

    /**
     * 查询产品规格列表
     */
    @RequiresPermissions("zsyh:partsattribute:list")
    @PostMapping("/listDetail")
    @ResponseBody
    public TableDataInfo listDetail(ZsyhPlatformPartsattribute zsyhPartsAttribute)
    {
        startPage();
        zsyhPartsAttribute.setOperateType("search");
        List<ZsyhPlatformPartsattribute> list= zsyhPlatformPartsattributeService.selectZsyhPlatformPartsattributeList(zsyhPartsAttribute);
        return getDataTable(list);
    }

    /**
     * 规格图片
     */
    @GetMapping("/avatarAttribute")
    public String avatarAttribute(String attributeLogo,ModelMap mmap)
    {
        mmap.put("avatar",attributeLogo);
        return prefix + "/avatarAttribute";
    }

    /**
     * 保存规格图片
     */
    @Log(title = "保存规格图片", businessType = BusinessType.UPDATE)
    @PostMapping("/updateAvatar")
    @ResponseBody
    public AjaxResult updateAvatar(@RequestParam("avatarfile") MultipartFile file)
    {
        try
        {
            if (!file.isEmpty())
            {
                String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, null);
                return new AjaxResult(AjaxResult.Type.SUCCESS,"上传成功",avatar);
            }
            return error();
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 置顶配件规格
     */
    @RequiresPermissions("zsyh:partsattribute:remove")
    @Log(title = "配件管理", businessType = BusinessType.DELETE)
    @PostMapping( "/moveTop")
    @ResponseBody
    public AjaxResult moveTop(String id)
    {
        ZsyhPlatformPartsattribute zsyhPlatformPartsattribute = zsyhPlatformPartsattributeService.selectZsyhPlatformPartsattributeById(id);
        ZsyhPlatformPartsattribute zsyhPlatformPartsattribute1 = new ZsyhPlatformPartsattribute();
        zsyhPlatformPartsattribute1.setPartsId(zsyhPlatformPartsattribute.getPartsId());
        List<ZsyhPlatformPartsattribute> zsyhPlatformPartsattributeList = zsyhPlatformPartsattributeService.selectZsyhPlatformPartsattributeList(zsyhPlatformPartsattribute1);
        Long attributeSort = zsyhPlatformPartsattributeList.get(0).getAttributeSort();
        zsyhPlatformPartsattribute.setAttributeSort(attributeSort+1);
        return toAjax(zsyhPlatformPartsattributeService.updateZsyhPlatformPartsattribute(zsyhPlatformPartsattribute));
    }

    /**
     * 上移,下移配件规格
     */
    @Transactional
    @RequiresPermissions("zsyh:partsattribute:edit")
    @Log(title = "配件管理", businessType = BusinessType.DELETE)
    @PostMapping( "/moveUp")
    @ResponseBody
    public AjaxResult moveUp(String id,String otherId,Long attributeSort,Long otherAttributeSort)
    {

        ZsyhPlatformPartsattribute zsyhPlatformPartsattribute = new ZsyhPlatformPartsattribute();
        ZsyhPlatformPartsattribute otherZsyhPlatformPartsattribute = new ZsyhPlatformPartsattribute();
        zsyhPlatformPartsattribute.setId(id);
        zsyhPlatformPartsattribute.setAttributeSort(otherAttributeSort);
        otherZsyhPlatformPartsattribute.setId(otherId);
        otherZsyhPlatformPartsattribute.setAttributeSort(attributeSort);
        int num = zsyhPlatformPartsattributeService.updateZsyhPlatformPartsattribute(zsyhPlatformPartsattribute);
        if(num >0){
            zsyhPlatformPartsattributeService.updateZsyhPlatformPartsattribute(otherZsyhPlatformPartsattribute);
        }
        return toAjax(num);
    }

    /**
     * 移动到指定位置
     */
    @Transactional
    @RequiresPermissions("zsyh:partsattribute:edit")
    @Log(title = "配件管理", businessType = BusinessType.DELETE)
    @PostMapping( "/moveChange")
    @ResponseBody
    public AjaxResult moveChange(String id,String oldId)
    {

        ZsyhPlatformPartsattribute zsyhPlatformPartsattribute = zsyhPlatformPartsattributeService.selectZsyhPlatformPartsattributeById(id);
        long newSort = zsyhPlatformPartsattribute.getAttributeSort();
        ZsyhPlatformPartsattribute oldZsyhPlatformPartsattribute = zsyhPlatformPartsattributeService.selectZsyhPlatformPartsattributeById(oldId);
        int num =0;
        if(oldZsyhPlatformPartsattribute.getAttributeSort()>zsyhPlatformPartsattribute.getAttributeSort()){
            //如果要移动的位置在当前位置之前,那么目标位置之后要都加1
            num = zsyhPlatformPartsattributeService.updateAttributeSort(1,zsyhPlatformPartsattribute.getPartsId(),zsyhPlatformPartsattribute.getAttributeSort());
            oldZsyhPlatformPartsattribute.setAttributeSort(newSort+1);
        }else{
            num = zsyhPlatformPartsattributeService.updateAttributeSortBefore(1,zsyhPlatformPartsattribute.getPartsId(),zsyhPlatformPartsattribute.getAttributeSort(),oldZsyhPlatformPartsattribute.getAttributeSort());
            oldZsyhPlatformPartsattribute.setAttributeSort(newSort);
        }
        zsyhPlatformPartsattributeService.updateZsyhPlatformPartsattribute(oldZsyhPlatformPartsattribute);
        return toAjax(num);
    }

}
